package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //向套餐表插入数据
        setmealMapper.insert(setmeal);
        //获取生成的套餐id
        Long id = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });

        //保存套餐和菜品的关联关系
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids).eq("status", StatusConstant.ENABLE);
        Long count = setmealMapper.selectCount(wrapper);
        if (count>0)
            throw new BaseException("正在出售的菜品不允许删除");
        setmealMapper.deleteByIds(ids);
        QueryWrapper<SetmealDish> wrapper1 = new QueryWrapper<>();
        wrapper.in("Setmeal_id", ids);
        setmealDishMapper.delete(wrapper1);
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        //1、修改套餐表，执行update
        setmealMapper.updateById(setmeal);
        //删除套餐中旧的菜品
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setmealDTO.getId());
        setmealDishMapper.delete(wrapper);
        //取出新的菜品并跟新
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        //起售时查看是否有停售菜品
        if (status==StatusConstant.ENABLE){
//            QueryWrapper<Dish> wrapper = new QueryWrapper<>();
//            wrapper.eq("setmeal_id",id).eq("status");
            Integer count = dishMapper.getDishCountBySetmealId(id);
            if (count>0)
                throw new BaseException("套餐内包含未启售菜品，无法启售");
        }
        UpdateWrapper<Setmeal> wrapper = new UpdateWrapper<>();
        wrapper.set("status",status).eq("id",id);
        setmealMapper.update(wrapper);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        if (setmeal.getName()!=null && !Objects.equals(setmeal.getName(), ""))
            wrapper.like("name",setmeal.getName());
        wrapper.eq("category_id",setmeal.getCategoryId());
        wrapper.eq("status",setmeal.getStatus());
        return setmealMapper.selectList(wrapper);
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
